package net.datatables.ajax;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class FormData {

	private String columnsData;
	private String columnsName;
	private boolean columnsSearchable = false;

	private boolean columnsOrderable = false;
	private String columnsSearchValue = "";
	private boolean columnsSearchRegex = false;

	private String orderDir;
	private int orderColumn;

	private int start;
	private int length;
	private String searchValue;
	private boolean searchRegex;
	Map<String, Object> param = new HashMap<String, Object>();

	public FormData(HttpServletRequest request) {
		if (request.getParameter("start") != null) {
			start = Integer.valueOf(request.getParameter("start"));
			length = Integer.valueOf(request.getParameter("length"));
		}
		orderDir = request.getParameter("order[0][dir]");
		if (orderDir != null) {
			if (orderDir.isEmpty()) {
				orderDir = null;
			}
			orderColumn = Integer.valueOf(request.getParameter("order[0][column]"));

			columnsData = request.getParameter("columns[" + orderColumn + "][data]");
			if (columnsData.isEmpty()) {
				columnsData = null;
			}
			columnsName = request.getParameter("columns[" + orderColumn + "][name]");
			if (columnsName.isEmpty()) {
				columnsName = null;
			}
			columnsSearchable = Boolean.parseBoolean(request.getParameter("columns[" + orderColumn + "][searchable]"));
			columnsOrderable = Boolean.parseBoolean(request.getParameter("columns[" + orderColumn + "][orderable]"));
			columnsSearchRegex = Boolean.parseBoolean(request.getParameter("columns[" + orderColumn + "][search][regex]"));
			columnsSearchValue = request.getParameter("columns[" + orderColumn + "][search][value]");
			if (columnsSearchValue.isEmpty()) {
				columnsSearchValue = null;
			}
		}

		searchValue = request.getParameter("search[value]");
		if (searchValue != null && searchValue.isEmpty()) {
			searchValue = null;
		}
		searchRegex = Boolean.parseBoolean(request.getParameter("search[regex]"));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
			String name = e.nextElement();
			//约定参数前缀
			String val = request.getParameter(name);
			if (val != null && !val.isEmpty()) {
				if (name.startsWith("q_time")) {
					try {
						param.put(name, df.parse(val));
					} catch (ParseException e1) {
						param.put(name, val);
					}
				} else {
					param.put(name, val);
				}
			}
		}
	}

	public String getColumnsData() {
		return columnsData;
	}

	public String getColumnsName() {
		return columnsName;
	}

	public boolean isColumnsSearchable() {
		return columnsSearchable;
	}

	public boolean isColumnsOrderable() {
		return columnsOrderable;
	}

	public String getColumnsSearchValue() {
		return columnsSearchValue;
	}

	public boolean isColumnsSearchRegex() {
		return columnsSearchRegex;
	}

	public String getOrderDir() {
		return orderDir;
	}

	public int getStart() {
		return start;
	}

	public int getLength() {
		return length;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public boolean isSearchRegex() {
		return searchRegex;
	}

	/**
	 * @return the param
	 */
	public Map<String, Object> getParam() {
		return param;
	}

	public Map<String, Object> putParam(String key, Object value) {
		param.put(key, value);
		return param;
	}

}
